//
//  CSGXBodyView.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/8.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXBodyView.h"
#import "CSGXBodyPageView.h"

@interface CSGXBodyView ()

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, weak) UIView *vSize;

@property (nonatomic, strong) NSArray<CSGXBodyPageView *> *pages;

@end

@implementation CSGXBodyView


#pragma mark - Actions
- (void)vPageAction:(CSGXBodyPageView *)sender {
    NSInteger page = sender.tag;
    NSInteger index = sender.selectedIndex;
    
#pragma mark call back
    if ([self.bodyViewDelegate respondsToSelector:@selector(bodyView:page:elementIndex:)]) {
        [self.bodyViewDelegate bodyView:self page:page elementIndex:index];
    }
}

#pragma mark - methods
- (NSInteger)initPage {
    NSInteger page = self.elementList.count;
    return page;
}



#pragma mark - setupUI
- (void)setupUI {
    self.bounces = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.pagingEnabled = YES;
}

- (void)initBodyView {
    //
    UIView *vSize = [[UIView alloc] init];
    _vSize = vSize;
    [self addSubview:self.vSize];
    
    [vSize mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.width.equalTo(self).multipliedBy(_page);
        make.height.equalTo(self);
    }];
    [self layoutIfNeeded];
    //
    if (self.page <= 0) {
        return;
    }
    
    NSMutableArray<CSGXBodyPageView *> *vpageArrM = [NSMutableArray<CSGXBodyPageView *> array];
    for (int i = 0; i < _page; i++) {
        CSGXBodyPageView *vPage = [[CSGXBodyPageView alloc] init];
        [self.vSize addSubview:vPage];
        
//        vPage.backgroundColor = [UIColor gx_randomColor];
        vPage.tag = i;
        vPage.titleList = self.elementList[i];
        [vPage addTarget:self action:@selector(vPageAction:) forControlEvents:UIControlEventValueChanged];
        
        [vpageArrM addObject:vPage];
    }
    
    [vpageArrM[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(_vSize).mas_offset(0);
        make.width.equalTo(self);
    }];
    
    if (vpageArrM.count >= 2) {
        for (int i = 1; i < vpageArrM.count; i++) {
            [vpageArrM[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.bottom.equalTo(vpageArrM[i-1]).mas_offset(0);
                make.leading.equalTo(vpageArrM[i-1].mas_trailing).mas_offset(0);
            }];
            
        } //layout for end
    }
    
    self.pages = vpageArrM.copy;
}


#pragma mark - setter & getter
- (void)setElementList:(NSArray<NSArray<NSString *> *> *)elementList {
    _elementList = elementList;
    self.page = [self initPage];
    
    [self initBodyView];
}







@end
